import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { ValidateProvider } from "../../providers/validate/validate";
import { StorageProvider } from "../../providers/storage/storage";

@IonicPage({
    segment:'member/my_message',
    defaultHistory:['MemberPage']
})
@Component({
    selector: 'page-person-my-message',
    templateUrl: 'person-my-message.html',
})
export class PersonMyMessagePage {
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService:HttpServicesProvider,
        public storage:StorageProvider,
        public validate:ValidateProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewWillEnter(){
        this.getData(null);
    }

    page = 1;
    lists = [];
    getData(infiniteScroll){
        let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
        let token = tokenInfo['token'];

        let apiUrl = "/message/my_message_list/token/"+token+"/page/"+this.page;
        this.httpService.requestData(apiUrl,(response) => {
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if(infiniteScroll){
                infiniteScroll.complete();   /*请求完成数据更新页面*/
                if(data['lists'].length<data['page_size']){   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }
}
